package com.onefish.toolkit;

import cn.hutool.setting.dialect.Props;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.*;

public class ThreadPoolExecutorDemo {
    private static final int corePoolSize = new Props("threadPoolExecutor.properties", StandardCharsets.UTF_8).getInt("businessThreadPool.corePoolSize",5);
    private static final int maximumPoolSize = new Props("threadPoolExecutor.properties", StandardCharsets.UTF_8).getInt("businessThreadPool.maximumPoolSize",5);
    private static final long workQueueSize = new Props("threadPoolExecutor.properties", StandardCharsets.UTF_8).getInt("businessThreadPool.workQueueSize",5);

    // TODO: 实现线程池功能
    public static ThreadPoolExecutor businessThreadPool = new ThreadPoolExecutor(
            corePoolSize,
            maximumPoolSize,
            workQueueSize,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(2), new ThreadPoolExecutor.AbortPolicy());
    public static void main(String[] args) {
        // 提交任务 由于队列为LinkedBlockingQueue，所以任务处理遵循FIFO原则
        businessThreadPool.submit(() -> System.out.println("Task 1 is running"));
        businessThreadPool.submit(() -> System.out.println("Task 2 is running"));
        businessThreadPool.submit(() -> System.out.println("Task 3 is running"));
        businessThreadPool.submit(() -> System.out.println("Task 4 is running"));
        businessThreadPool.submit(() -> System.out.println("Task 5 is running"));
        // 关闭线程池
        businessThreadPool.shutdown();
    }
}
